package com.cgl.service.imp;

import com.cgl.mapper.ArticleMapper;
import com.cgl.mapper.ReadingRecordMapper;
import com.cgl.mapper.UserActionMapper;
import com.cgl.pojo.Article;
import com.cgl.pojo.ReadingRecord;
import com.cgl.service.ArticleService;
import com.cgl.until.TextDocmentReadAndWriting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
@Service
public class ArticleServiceImp implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ReadingRecordMapper readingRecordMapper;
    @Autowired
    private UserActionMapper userActionMapper;
    @Override
    public int addArticle(Article article) {
        //36位随机id
        String id = UUID.randomUUID().toString().replace("-", "");
        article.setId(id);

        //正式发布
        article.setStatus("1");

        String flileName = id+".txt";

        TextDocmentReadAndWriting textDocmentReadAndWriting = new TextDocmentReadAndWriting();

        boolean isWrite = textDocmentReadAndWriting.write(flileName,article.getContent());

        if (!isWrite){
            return 2;
        }

        article.setContent(flileName);

        article.setLikeCount(0);
        article.setCommentCount(0);
        article.setViewCount(0);
        article.setCreatedTime(LocalDate.now());
        article.setUpdateTime(LocalDate.now());
        article.setUploadTime(LocalDate.now());

        return articleMapper.add(article);
    }

    @Override
    public int addArticleByDraft(Article article) {
        //36位随机id
        String id = UUID.randomUUID().toString().replace("-", "");
        article.setId(id);

        //草稿箱
        article.setStatus("2");

        //存到本地
        String flileName = id+".txt";

        TextDocmentReadAndWriting textDocmentReadAndWriting = new TextDocmentReadAndWriting();

        boolean isWrite = textDocmentReadAndWriting.write(flileName,article.getContent());

        if (!isWrite){
            return 2;
        }

        article.setContent(flileName);

        article.setLikeCount(0);
        article.setCommentCount(0);
        article.setViewCount(0);
        article.setCreatedTime(LocalDate.now());
        article.setUpdateTime(LocalDate.now());
        article.setUploadTime(LocalDate.now());

        return articleMapper.add(article);
    }

    @Override
    public List<Article> getArticl(int pageIndex, int pageSize) {
        List<Article> articles = articleMapper.selectAll("1", pageIndex, pageSize);
        return articles;
    }

    @Override
    public List<Article> getDraft(int pageIndex, int pageSize) {
        List<Article> articles = articleMapper.selectAll("2", pageIndex, pageSize);
        return articles;
    }

    @Override
    public int getAllArticleCount() {
        return articleMapper.getCount("1");
    }

    @Override
    public int getAllDraftCount() {
        return articleMapper.getCount("2");
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Article getArticleById(String id) {
        Boolean isOK= articleMapper.addviewCount(id);

        Article article = articleMapper.selectById(id);

        TextDocmentReadAndWriting textDocmentReadAndWriting = new TextDocmentReadAndWriting();

        String content =  textDocmentReadAndWriting.read(article.getContent());

        article.setContent(content);

        return article;
    }

    @Override
    public List<Article> getArticleByLast(int pageIndex, int pageSize) {
        int  start = (pageIndex-1)*pageSize;
        List<Article> articles = articleMapper.selectAllByLast("1", start, pageSize);
        return articles;
    }

    @Override
    public Boolean addviewCount(String id) {
        return articleMapper.addviewCount(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addLikeCount(String userid,String id) {
        try {
            // 添加阅读记录
            ReadingRecord readingRecord = new ReadingRecord(userid, id, LocalDateTime.now(), 0, 0);
            Boolean result = readingRecordMapper.addReadingRecord(readingRecord);
            Boolean isLike = articleMapper.addLikeCount(id);
            return (result && isLike);
        } catch (Exception e) {
            // 如果出现异常，事务会自动回滚
            // 可以在这里记录日志
            e.printStackTrace();
            return false;
        }
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public HashMap<String, Object> getInfo() {
        HashMap<String, Object> map = new HashMap<>();

        map.put("allLikeCount", articleMapper.getAllLikeCount());
        map.put("allViewCount", articleMapper.getAllViewCount());
        map.put("allCount", userActionMapper.getCount());

        return map;
    }

    @Override
    public List<Article> getArticleRank() {
        return articleMapper.getArticleRank("1");
    }

    @Override
    public List<Article> getArticleLastRank() {
        return articleMapper.getArticleLastRank("1");
    }

    @Override
    public List<Article> getArticleLikeRank() {
        return articleMapper.getArticleLikeRank("1");
    }

    @Override
    public List<Article> getAllArticle(int pageIndex, int pageSize) {
        int  start = (pageIndex-1)*pageSize;
        List<Article> articles = articleMapper.getAllArticle(start, pageSize);
        return articles;
    }

    @Override
    public int deleteArticle(String id) {
        int result = articleMapper.updateStatus(id, "3");
        return result;
    }

    @Override
    public int updataArticle(Article article) {
        TextDocmentReadAndWriting textDocmentReadAndWriting = new TextDocmentReadAndWriting();

        String flileName = article.getId()+".txt";

        boolean isWrite = textDocmentReadAndWriting.write(flileName,article.getContent());

        if (!isWrite){
            return 0;
        }

        int result = articleMapper.update(article);

        return result;
    }
}         
